package com.tyrant.macaulottery;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.tyrant.macaulottery.activity.ShowActivityActivity;
import com.tyrant.macaulottery.adapter.BaseRecyclerAdapter;
import com.tyrant.macaulottery.adapter.BaseRecyclerViewHolder;
import com.tyrant.macaulottery.bean.PushBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.Sort;


/**
 * A simple {@link Fragment} subclass.
 */
public class MessageFragment extends Fragment {


    ArrayList<PushBean> mdata = new ArrayList<PushBean>();
    BaseRecyclerAdapter adapter;
    @Bind(R.id.swipe_target)
    RecyclerView swipe_target;
    @Bind(R.id.iv_noface)
    ImageView iv_noface;
    private Realm realm;

    public MessageFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        swipe_target.setLayoutManager(layoutManager);//设置显示方式
        adapter = new BaseRecyclerAdapter<PushBean>(getActivity(), mdata, R.layout.message_item) {
            @Override
            public void init(BaseRecyclerViewHolder holder, PushBean pushBean, int position) {
                holder.setText(R.id.tv_time, pushBean.getTime());
                holder.setText(R.id.tv_content, pushBean.getContent());
            }
        };
        adapter.setOnItemClickLinster(new BaseRecyclerAdapter.OnItemClickLinster() {
            @Override
            public void onItemClick(View view, int position) {
//                Toast.makeText(getActivity(), "mdata.get(position):" + mdata.get(position).getUrl(), Toast.LENGTH_SHORT).show();
//                Intent intent = new Intent(getActivity(), ShowActivityActivity.class);
//                intent.putExtra("data", mdata.get(position).getActivity());
//                startActivity(intent);
                Intent intent = new Intent(getActivity(), ShowActivityActivity.class);
                intent.putExtra("data", mdata.get(position).getContent());
                startActivity(intent);
            }
        });
        swipe_target.setAdapter(adapter);//设置适配器

        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
        realm = Realm.getInstance(realmConfiguration);

        List<PushBean> allPushBean = realm.where(PushBean.class).findAllSorted("time", Sort.DESCENDING);
        mdata.clear();
        mdata.addAll(allPushBean);
        if (mdata.size() == 0) {
            iv_noface.setVisibility(View.VISIBLE);
            swipe_target.setVisibility(View.GONE);
        }else {
            iv_noface.setVisibility(View.GONE);
            swipe_target.setVisibility(View.VISIBLE);
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
